package cn.longge.mapper;

import cn.longge.entity.SysArea;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

public class SysAreaSqlProvider {

    public String selectByPage(Map<String,Object> params){
        return new SQL(){
            {
                SELECT("parent.name parent_name,sub.id,sub.parent_id,sub.parent_ids,sub.`code`,sub.type,sub.name,sub.icon");
                FROM("sys_area parent,sys_area sub");
                if(params.containsKey("parentId") && !StringUtils.isEmpty(params.get("parentId"))){
                    WHERE("(sub.parent_ids LIKE concat( '%,', #{parentId}, ',%' ) or sub.id = #{parentId})");
                }else if(params.containsKey("parentName") && !StringUtils.isEmpty(params.get("parentName"))){
                    WHERE("sub.name LIKE concat( '%', #{parentName}, '%' )");
                }
                WHERE("parent.id = sub.parent_id");
            }
        }.toString();
    }

    public String insertMany(@Param("areas") List<SysArea> areas){
        return new SQL(){
            {
                INSERT_INTO("sys_area");
                INTO_COLUMNS("`parent_id`, `parent_ids`, `code`, `name`, `type`, `create_by`, `create_date`, " +
                        "`update_by`, `update_date`, `remarks`, `del_flag`, `icon`");
                for (int i = 0; i < areas.size(); i++) {
                    INTO_VALUES("#{areas[" + i + "].parentId}");
                    INTO_VALUES("#{areas[" + i + "].parentIds}");
                    INTO_VALUES("#{areas[" + i + "].code}");
                    INTO_VALUES("#{areas[" + i + "].name}");
                    INTO_VALUES("#{areas[" + i + "].type}");
                    INTO_VALUES("#{areas[" + i + "].createBy}");
                    INTO_VALUES("#{areas[" + i + "].createDate}");
                    INTO_VALUES("#{areas[" + i + "].updateBy}");
                    INTO_VALUES("#{areas[" + i + "].updateDate}");
                    INTO_VALUES("#{areas[" + i + "].remarks}");
                    INTO_VALUES("#{areas[" + i + "].delFlag}");
                    INTO_VALUES("#{areas[" + i + "].icon}");
                    ADD_ROW();
                }
            }
        }.toString();
    }
}
